//
//  ChannelViewController.m
//  CapriceMusic
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "ChannelViewController.h"
#import "ChannelCollectionViewCell.h"
#import "ChannelModel.h"
#import "AppDelegate.h"
#import "ChannelDetailViewController.h"
@interface ChannelViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *modelArray;
@end

@implementation ChannelViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"频道";
    self.modelArray = [NSMutableArray arrayWithCapacity:0];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2, self.view.frame.size.height / 4);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = CGFLOAT_MIN;
    flowLayout.minimumLineSpacing = CGFLOAT_MIN;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerClass:[ChannelCollectionViewCell class] forCellWithReuseIdentifier:@"ChannelCollectionViewCellIdentifier"];
    [self.view addSubview:self.collectionView];
    [self getData:nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChannelCollectionViewCellIdentifier" forIndexPath:indexPath];
    if (self.modelArray.count) {
        ChannelModel *model = self.modelArray[indexPath.item];
        [cell.channelImageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
        cell.channelLabel.text = model.title;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ChannelDetailViewController *detail = [[ChannelDetailViewController alloc] init];
    detail.channelModel = self.self.modelArray[indexPath.item];
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)getData:(NSString *)str {
    [ProgressHUD show:@"正在加载"];
    [self.modelArray removeAllObjects];
    [JEHNetWorkTool getWithUrl:ChannelPageGETURL parameter:nil httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"data"];
        for (NSDictionary *dic in arr) {
            ChannelModel *channelModel = [[ChannelModel alloc] initWithDictionary:dic];
            [self.modelArray addObject:channelModel];
        }
        [ProgressHUD dismiss];
        [_collectionView reloadData];
    } fail:^(NSError *error) {
        [ProgressHUD showError:@"请检查网络"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
